home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / ATOF.C < prev    next >
Text File  |  1986-05-18  |  3KB  |  114 lines

  1. /* 1.2  10-08-85                        (atof.c) 
  2.  ************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1985        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10. #include "mathcons.h"
  11. #include "mathtyp.h"
  12. #include "ctype.h"
  13.  
  14. /************************************************************************/
  15.     STRING
  16. astof(s, val)    /* convert value of string s in double/float format and 
  17.            store in val.  return pointer to next unused char.    */
  18. /*----------------------------------------------------------------------*/
  19. STRING s;
  20. double *val;
  21. {
  22.     STRING astoi();
  23.     double v, fr, tenpow();
  24.     int i;
  25.     BOOL sign;
  26.     char c;
  27.  
  28.     while (isspace(c = *s))
  29.         s++;
  30.     if (sign = (c IS '-'))
  31.         s++;
  32.     else if (c IS '+')
  33.         s++;
  34.     for (v = 0.0; isdigit(*s); v = 10. * v + (*s++ - '0'));
  35.     if (*s IS '.')
  36.         for (fr = 10.0, s++; isdigit(*s); fr *= 10.)
  37.             v += (*s++ - '0') / fr;
  38.     while (isspace(*s))
  39.         s++;
  40.     if (TOLOWER(*s) IS 'e')
  41.     {    if ((i = ((c = *(++s)) IS '-')) OR c IS '+')
  42.             s++;
  43.         while (*s IS '0')
  44.             s++;
  45.         if (i)
  46.             *(--s) = '-';
  47.         s = astoi(s, &i);
  48.         v *= tenpow(i);
  49.     }
  50.     *val = (sign ? -v : v);
  51.     return (s);
  52. }
  53. /*\p*********************************************************************/
  54.     double
  55. atof(s)        /* return value of the string s in double/float format    */
  56.  
  57. /*----------------------------------------------------------------------*/
  58. STRING s;
  59. {
  60.     double x;
  61.     STRING astof();
  62.  
  63.     astof(s, &x);
  64.     return x;    
  65. }
  66.  
  67. /************************************************************************/
  68.     LOCAL double
  69. tenpow(n)        /* Return 10.0^n, or -1 on error.        */
  70.  
  71. /*----------------------------------------------------------------------*/
  72. {
  73.     /********************************************************
  74.      * This array contains 10^(2^n) up to n = BIG10X-1    *
  75.      ********************************************************/
  76.     LOCAL double tentothe2tothe[BIG10X] =
  77.     {    10., 100., 10000., 100000000., 10000000000000000.,
  78.         100000000000000000000000000000000.
  79. #ifdef AZTEC
  80.         , 1.0e64, 1.0e128
  81. #endif
  82.  
  83. #ifdef SUN
  84.         , 1.0e64, 1.0e128, 1.0e256
  85. #endif
  86.     };
  87.  
  88.     /********************************************************
  89.      * Also, make this array be 2^n up to 2^(BIG10X-1)    *
  90.      ********************************************************/
  91.     LOCAL int twoto[BIG10X] =
  92.     {    1, 2, 4, 8, 16, 32
  93. #ifdef AZTEC
  94.         , 64, 128
  95. #endif
  96.  
  97. #ifdef SUN
  98.         , 64, 128, 256
  99. #endif
  100.     };
  101.  
  102.     int i, minus;
  103.     double d;
  104.  
  105.     if (minus = (n < 0) ? TRUE : FALSE)
  106.         n = -n;
  107.     for (d = 1.0, i = BIG10X; --i >= 0; )
  108.         if (n >= twoto[i])
  109.         {    d *= tentothe2tothe[i];
  110.             n -= twoto[i];
  111.         }
  112.     return (minus) ? 1.0 / d : d;
  113. }
  114.